<?php

namespace backend\controllers;

use Yii;
use common\models\Category;
use yii\base\Object;
use yii\helpers\Json;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * CategoryController implements the CRUD actions for Category model.
 */
class CategoryController extends DashboardController
{
    /**
     * Lists all Category models.
     * @return mixed
     */
    public function actionIndex()
    {
        $cate = Category::getAllCategory();
        $data = array();
        return $this->render('index',compact('cate',$data));
    }

    /**
     * Creates a new Category model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Category();

        if (Yii::$app->request->isPost) {
            $params = Yii::$app->request->post();

            $model->cname = $params['cname'];
            $model->desc = $params['desc'];
            $model->pid = isset($params['pid'])?$params['pid']:0;

            if ($model->save()){
                return $this->redirect('index');
            }
        } else {
            $id =Yii::$app->request->get("id");
            if (isset($id)){
                $info =$this->findModel($id);
                return $this->render('create',['model'=>$info]);
            }else{
                return $this->render('create');
            }
        }
    }

    /**
     * Updates an existing Category model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if (Yii::$app->request->isPost) {
            $params = Yii::$app->request->post();

            $model->cname = $params['cname'];
            $model->desc = $params['desc'];

            if ($model->save()){
                return $this->redirect('index');
            }
        } else {
            //查询其父类的信息
            $pid = $model->pid;
            if ($pid){
                $pmodel =$this->findModel($pid);
            }else{
                $pmodel = new Object();
            }
            return $this->render('edit', [
                'model' => $model,
                'pmodel' =>$pmodel,
            ]);
        }
    }

    /**
     * Deletes an existing Category model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete()
    {
        $id = Yii::$app->request->post('id');
        $model = new Category();
        if($item = $model->findOne($id)){
            $transaction = Yii::$app->db->beginTransaction();
            //$item->removeAssignments();
            if($item->delete()){
                $transaction->commit();
                return Json::encode([
                    'state' => 1,
                    'message' => '删除成功！',
                ]);
            }
            $transaction->rollBack();
            return Json::encode([
                'state' => 0,
                'message' => '删除失败！'.implode('',array_flatten($item->getErrors())),
            ]);
        }else{
            return Json::encode([
                'state' => 0,
                'message' => '数据不存在！',
            ]);
        }
    }

    /**
     * Finds the Category model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Category the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Category::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
